package com.boot.message.netty.client.sender.impl;

import com.boot.message.netty.client.NettyClientProperties;
import com.boot.message.netty.client.handler.AbstractClientChannelInitializer;
import com.boot.message.netty.client.sender.NettyClientSender;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.socket.SocketChannel;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @date 2020/3/13 23:34
 **/

public class JSONSender extends NettyClientSender {

    private final ObjectMapper om;

    public JSONSender(NettyClientProperties nettyClientProperties, AbstractClientChannelInitializer<SocketChannel> clientChannelInitializer, ObjectMapper om) {
        super(nettyClientProperties, clientChannelInitializer);
        this.om = om;
    }

    @Override
    public NettyClientSender write(String data) {
        return super.write(data + '|');
    }

    public NettyClientSender write(Object o) {
        try {
            String s = om.writeValueAsString(o);
            return this.write(s);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
